package crm.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginCheckFilter implements Filter{
    public void init(FilterConfig filterConfig)throws ServletException{}
    public void destroy(){}
    public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain)
        throws IOException,ServletException
    {
        HttpServletRequest req=(HttpServletRequest)request;
        HttpServletResponse res=(HttpServletResponse)response;
        response.setContentType("text/html;charset=UTF-8"); 
        request.setCharacterEncoding("UTF-8");//设置字符编码
        response.setCharacterEncoding("UTF-8");
        res.setHeader("Pragma", "No-cache");//禁止缓存
        res.setHeader("Cache-Control","no-cache");
        res.setHeader("Expires", "0");
        PrintWriter out=res.getWriter();
        HttpSession session=req.getSession();
        if(session.getAttribute("user")!=null){
            chain.doFilter(request, response);
        }
        else{
        	String pathtt = ((HttpServletRequest) request).getContextPath();
            String basePathxbc = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+pathtt+"/";
        	String logout =basePathxbc+"pages/login/login.html";
        	//System.out.println(logout+"EEEE");
               out.println(" <script  type='text/javascript'>alert('您尚未登陆，若要继续浏览，请先登陆。'); </script>");
               ((HttpServletResponse) response).setHeader("refresh","0;URL="+logout) ;
        }
    }

}